<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
 /*********************************************
傳入參數
Account：帳號
Password：密碼
sid：隨機亂數
***********************************************/
set_time_limit(0);
include_once("../config.inc.php");

$arrSIP = split('\.', $_SERVER["SERVER_ADDR"]);
$arrRIP = split('\.', $_SERVER["REMOTE_ADDR"]);
if($arrSIP[0] != $arrRIP[0] || $arrSIP[1] != $arrRIP[1] || $arrSIP[2] != $arrRIP[2]){
	if($_SERVER["HTTP_HOST"] != "lottou.uuss.net")exit("No Power!! <!--".$_SERVER["SERVER_ADDR"]."=".$_SERVER["REMOTE_ADDR"]."//-->");
}
//print "<!--".$_SERVER["SERVER_ADDR"]."=".$_SERVER["REMOTE_ADDR"]."//-->";

session_start();
//$_SESSION["DEBUG"] = 1;
$_LangPath = "login_member";	//語言檔參數，務必放在最前面

include(__Language_Path."/selectlanguage_member.php");
include(__DBConnect_File);
include(__Modules_Path."/LogRecord.lib.php");
include(__Modules_Path."/Login.lib.php");
include(__Common_Path."/SysParam.inc.php");
include(__Common_Path."/Lotto.inc.php");

$strAccount = trim($_data["Account"]);
$strPWD = trim($_data["Password"]);
$strBT = trim($_data["Bet_Type"]);
$intNum = (int)$_data["Number"];
$intGID = (int)$_data["GambleID"];
$intLottoID = (int)$_data["LottoID"];
$intLType = (int)$_arrLotto[$intLottoID]["LType"];

if($intLType == 1 && ($strBT == "1" || $strBT == "28"))$strBT = "1,28";

//找出該會員資料
$strSQL = "select * from `bet_Member` where Account = '".$strAccount."'";
//print($strSQL);
//SQL 指令出錯
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$_SESSION["Member_LoginErrNo"]++;
	AddLogin_Log($strAccount."--Supple", $strPWD, 0);
	$GLOBALS["errormessage"] = $_strLogin["ID&PWD_Err"]."<br />".$_SESSION["Member_LoginErrNo"]." times!!";	//帳號和密碼錯誤，請重新登入！
	ErrMsgOutputHTML("Error1", "", 1);
}

//找不到帳號密碼
if(PMA_mysql_num_rows($RS) == 0){
	@mysql_free_result($RS);
	AddLogin_Log($strAccount."--Supple", $strPWD, 0);
	$GLOBALS["errormessage"] = $_strLogin["ID&PWD_Err"]."<br />".$_SESSION["Member_LoginErrNo"]." times!!";	//帳號和密碼錯誤，請重新登入！
	ErrMsgOutputHTML("Error2", "", 1);
}

$objMember = PMA_mysql_fetch_object($RS);
@mysql_free_result($RS);
if($objMember -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	ErrMsgOutputHTML("Error3", "", 1);
}

$objMember -> ERID = 1;
$objMember -> GambleType = 1;
$flMemERate = 1;
//===========================================================================//
$strSQLP = "";
$intPNo = -1;
switch($objMember -> MemberGroup){
	case 2:
		$objMember -> SubID = $objMember -> ID;
		//$objMember -> SubIsAutoSupple = $objMember -> isAutoSupple;
		$objMember -> WagerType = 2;
		$objMember -> SubScale = 1;
		$objMember -> MainScale = 1 - $objMember -> Rate2;
		$intPNo = 3;
		$strSQLP = "select * from `bet_Member` where ID in (".$objMember -> MainID.", ".$objMember -> StockID.", ".$objMember -> DirectorID.") order by MemberGroup limit 0, 3";
		$strSQLCR = "select SUM(MemStake*(1 - SubRate)*MemExchangeRate) as tolStake from `bet_BetListTmp` where SubID = ".$objMember -> ID." and WagerType = 0 and Bet_Type in (".$strBT.") and GambleID = ".$intGID;
		break;

	case 3:
		$objMember -> SubID = $objMember -> ID;
		$objMember -> MainID	= $objMember -> ID;
		//$objMember -> MainIsAutoSupple = $objMember -> isAutoSupple;
		$objMember -> SubScale = 1;
		$objMember -> MainScale = 1;
		$objMember -> StockScale = 1 - $objMember -> Rate1;
		$objMember -> WagerType = 3;
		$intPNo = 2;
		$strSQLP = "select * from `bet_Member` where ID in (".$objMember -> StockID.", ".$objMember -> DirectorID.") order by MemberGroup limit 0, 3";
		$strSQLCR = "select SUM(MemStake*(SubRate - MainRate)*MemExchangeRate) as tolStake from `bet_BetListTmp` where MainID = ".$objMember -> ID." and WagerType in (0, 2) and Bet_Type in (".$strBT.") and GambleID = ".$intGID;
		break;

	case 4:
		$objMember -> DirectorMaxScale = $objMember -> Rate2; //總監最多佔成
		$objMember -> SubID = $objMember -> ID;
		$objMember -> MainID = $objMember -> ID;
		$objMember -> StockID = $objMember -> ID;
		//$objMember -> StockIsAutoSupple = $objMember -> isAutoSupple;
		$objMember -> SubScale = 1;
		$objMember -> MainScale = 1;
		$objMember -> StockScale = 1;
		$objMember -> DirectorScale = 1 - $objMember -> Rate1;
		$objMember -> WagerType = 4;
		$intPNo = 1;
		$strSQLP = "select * from `bet_Member` where ID in (".$objMember -> DirectorID.") order by MemberGroup limit 0, 3";
		$strSQLCR = "select SUM(MemStake*(MainRate - StockRate)*MemExchangeRate) as tolStake from `bet_BetListTmp` where StockID = ".$objMember -> ID." and WagerType in (0, 2, 3) and Bet_Type in (".$strBT.") and GambleID = ".$intGID;
		break;

	case 5:
		$objMember -> SubID = $objMember -> ID;
		$objMember -> MainID = $objMember -> ID;
		$objMember -> StockID = $objMember -> ID;
		$objMember -> DirectorID = $objMember -> ID;
		//$objMember -> DirectorIsAutoSupple = $objMember -> isAutoSupple;
		$objMember -> SubScale = 1;
		$objMember -> MainScale = 1;
		$objMember -> StockScale = 1;
		$objMember -> DirectorScale = 1;
		$objMember -> WagerType = 5;
		$intPNo = 0;
		$strSQLCR = "select SUM(MemStake*(StockRate - DirectorRate)*MemExchangeRate) as tolStake from `bet_BetListTmp` where DirectorID = ".$objMember -> ID." and WagerType in (0, 2, 3, 4) and Bet_Type in (".$strBT.") and GambleID = ".$intGID;
		break;

	default:
		AddLogin_Log($strAccount."--Supple", $strPWD, -7);
		$GLOBALS["errormessage"] = $_strLogin["NotMember"];	//此帳號不屬於會員
		ErrMsgOutputHTML("Error4", "", 1);
		break;
} //switch($objMember -> MemberGroup){

if($intLType != 1){
	$strSQLCR .= " and Number = ".$intNum;
}else{
	switch($strBT){
		case "7":
		case "8":
		case "9":
		case "10":
		case "11":
		case "31":
		case "32":
		case "33":
		case "34":
		case "35":
		case "36":
		case "37":
		case "38":
		case "39":
		case "40":
		case "41":
		case "42":
		case "43":
			break;

		default:
			$strSQLCR .= " and Number = ".$intNum;
			break;
	}
}

$arrLayer[3] = array(2 => 1, 3 => 1, 4 => 1, 5 => 1);
$arrLayer[4] = array(2 => 1, 3 => 1, 4 => 1, 5 => 1);
$arrLayer[5] = array(2 => 1, 3 => 1, 4 => 1, 5 => 1);
//找出上層資料
if($strSQLP != ""){
	//print $strSQLP;
	if(!$RS = PMA_mysql_query($strSQLP, $$_Conn)){
		$GLOBALS["errormessage"] = "Find Parent Data Error!!";
		$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error5", "", 1);
	}
	if(PMA_mysql_num_rows($RS) < $intPNo){
		$GLOBALS["errormessage"] = "Not Find Agent Data!";
		$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error6", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		//此帳號停用
		if($Rows -> isLock == 1){
			AddLogin_Log($strAccount.$strSuppleID, $strPWD, $Rows -> MemberGroup*-1);
			$GLOBALS["errormessage"] = $_strLogin["NoPower"];	//您的帳號已經被停用！
			ErrMsgOutputHTML("Error7", "", 1);
		}
		$objMember -> LayerAcc[$Rows -> MemberGroup] = $Rows -> Account;
		switch($Rows -> MemberGroup){
			case 3:
				$objMember -> MainIsAutoSupple = $Rows -> isAutoSupple;
				$objMember -> MainAccount = $Rows -> Account;
				$objMember -> StockScale = $objMember -> MainScale - $Rows -> Rate1;
				$arrLayer[3][4] = 1 - $Rows -> Rate1;
				break;

			case 4:
				$objMember -> StockIsAutoSupple = $Rows -> isAutoSupple;
				$objMember -> StockAccount = $Rows -> Account;
				$objMember -> DirectorScale = $objMember -> StockScale - $Rows -> Rate1;
				$objMember -> DirectorMaxScale = $Rows -> Rate2; //總監最多佔成
				$arrLayer[3][5] = $arrLayer[3][4] - $Rows -> Rate1;
				$arrLayer[4][5] = 1 - $Rows -> Rate1;
				$arrLayer[5][5] = 1;
				break;

			case 5:
				$objMember -> DirectorIsAllowMaxRate = $Rows -> isAllowMaxRate;
				$objMember -> DirectorIsAutoSupple = $Rows -> isAutoSupple;
				$objMember -> DirectorAccount = $Rows -> Account;
				if($Rows -> isAllowMaxRate){
					$flTmp = $objMember -> StockScale - (1 - $Rows -> Rate1); // 剩餘成數 = 100 - 代理商 - 總代理 - 股東 - 公司最小成數
					if($flTmp > $objMember -> DirectorMaxScale)$flTmp = $objMember -> DirectorMaxScale; //若剩餘成數大於總監想要最多佔成
					$objMember -> DirectorScale = $objMember -> StockScale - $flTmp;

					$flTmp = $arrLayer[3][4] - (1 - $Rows -> Rate1); // 剩餘成數 = 100 - 股東 - 公司最小成數
					if($flTmp > $objMember -> DirectorMaxScale)$flTmp = $objMember -> DirectorMaxScale; //若剩餘成數大於總監想要最多佔成
					$arrLayer[3][5] = $arrLayer[3][4] - $flTmp;

					$flTmp = $arrLayer[4][4] - (1 - $Rows -> Rate1); // 剩餘成數 = 100 - 公司最小成數
					if($flTmp > $objMember -> DirectorMaxScale)$flTmp = $objMember -> DirectorMaxScale; //若剩餘成數大於總監想要最多佔成
					$arrLayer[4][5] = $arrLayer[4][4] - $flTmp;
				}
				break;
		} //switch($objMember -> MemberGroup){
	}
} //if($strSQLP != ""){
$objMember -> LayerScale = $arrLayer;

if(!$RS = PMA_mysql_query($strSQLCR, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Limit Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error8", "", 1);
}
$Rows = @mysql_fetch_object($RS);
$objMember -> CreditLine = $objMember -> SubCreditLine = $objMember -> MainCreditLine = $Rows -> tolStake;
//print "<!--".$objMember -> CreditLine." = ".$objMember -> SubCreditLine." = ".$objMember -> MainCreditLine." = . ".$strSQLCR."//-->";

//===========================================================================//
//$intBTNo = 28; //所有球類下注種類數量
//找出會員單場限額、單注限額
$strSQL = "(select * from `bet_MemberLimit` where MemID = ".$objMember -> ID." and Bet_Type in (".$strBT.") and LottoID = ".$intLottoID." and LimitType in ('0', '1')) union ";
$strSQL .= "(select * from `bet_MemberLimit` where MemID in (".$objMember -> SubID.",".$objMember -> MainID.",".$objMember -> StockID.",".$objMember -> DirectorID.") and Bet_Type in (".$strBT.") and LottoID = ".$intLottoID." and LimitType in ('2', '3'))";

if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Limit Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error9", "", 1);
}
$arrEventLimit = array();
$arrBetLimit = array();
$arrAcceptLimit = array();
while($Rows = @mysql_fetch_object($RS)){
	if($Rows -> LimitType == 0){
		$arrEventLimit[$Rows -> LottoID][$Rows -> Bet_Type] = 100000000;//floor($Rows -> LimitValue / $flMemERate);
	}elseif($Rows -> LimitType == 1){
		$arrBetLimit[$Rows -> LottoID][$Rows -> Bet_Type] = floor($Rows -> LimitValue / $flMemERate);
	}elseif($Rows -> LimitType == 2){
		$arrAcceptLimit[$Rows -> LottoID][$Rows -> Bet_Type][$Rows -> MemID] = $Rows -> LimitValue;
	}elseif($Rows -> LimitType == 3){
		$arrAutoSuppleLimit[$Rows -> LottoID][$Rows -> Bet_Type][$Rows -> MemID] = $Rows -> LimitValue;
	}
}

$objMember -> arrEventLimit = $arrEventLimit;
$objMember -> arrBetLimit = $arrBetLimit;
$objMember -> arrAcceptLimit = $arrAcceptLimit;
$objMember -> arrAutoSuppleLimit = $arrAutoSuppleLimit;

//===========================================================================//
//找出會員、代理商、總代理、股東退水
$strSQL = "select * from `bet_Commission` where MemID in (".$objMember -> ID.",".$objMember -> SubID.",".$objMember -> MainID.",".$objMember -> StockID.",".$objMember -> DirectorID.",".$objMember -> Layer6ID.") and Gamble_Type = 1 and Bet_Type in (".$strBT.") and LottoID = ".$intLottoID;
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = $_strLogin["NoCommission"]."1";  //"會員資料錯誤，其退水尚未設定！"
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error10", "", 1);
}

$intComm = 0;
while($Rows = @mysql_fetch_object($RS)){
	$arrCommission[$Rows -> MemID][$Rows -> LottoID][$Rows -> Bet_Type] = $Rows -> Commission;
	$intComm++;
}
$objMember -> arrCommission[1] = $arrCommission[$objMember -> ID];
$objMember -> arrCommission[2] = $arrCommission[$objMember -> SubID];
$objMember -> arrCommission[3] = $arrCommission[$objMember -> MainID];
$objMember -> arrCommission[4] = $arrCommission[$objMember -> StockID];
$objMember -> arrCommission[5] = $arrCommission[$objMember -> DirectorID];
$objMember -> arrCommission[6] = $arrCommission[$objMember -> Layer6ID];
//$intComm = sizeof($arrCommission[$objMember -> ID]) + sizeof($arrCommission[$objMember -> SubID]) + sizeof($arrCommission[$objMember -> MainID]) + sizeof($arrCommission[$objMember -> StockID]) + sizeof($arrCommission[$objMember -> DirectorID]);

/*if($intComm < ($intBTNo*5)){
	$GLOBALS["errormessage"] = $_strLogin["NoCommission"]."3";  //"會員資料錯誤，其退水尚未設定！"
	$GLOBALS["errorhidden"] = "Error: ".$intComm."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	include(__Root_Path."/error.php");
	exit();
}*/

$_SESSION["Member_Info"] = $objMember;
//print "\n\n";
//print_r($objMember);
//exit("\n\n");
//===========================================================================//
switch($intLType."_".$_data["Bet_Type"]){
	case "1_7":
	case "1_8":
	case "1_9":
	case "1_10":
	case "1_11":
		include("joinnumbercheckout_insert.php");
		break;

	case "1_31":
	case "1_32":
	case "1_33":
	case "1_34":
	case "1_35":
	case "1_36":
	case "1_37":
	case "1_38":
	case "1_39":
	case "1_40":
	case "1_41":
	case "1_42":
	case "1_43":
		include("10m15wager_insert.php");
		break;

	default:
		include("wager_insert.php");
		break;
}
?>